package com.huaxin.device.mapping;

import com.huaxin.device.dto.enums.BusbarMaterial;
import com.huaxin.device.dto.enums.ProtectionLevel;
import com.huaxin.device.dto.request.SwitchCabinetCreateReqDTO;
import com.huaxin.device.dto.request.SwitchCabinetUpdateReqDTO;
import com.huaxin.device.models.SwitchCabinet;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;

@Mapper(componentModel = "spring")
public interface SwitchCabinetMapping {

    @Mapping(target = "busbarMaterial", ignore = true)
    @Mapping(target = "protectionLevel", ignore = true)
    SwitchCabinet toEntity(SwitchCabinetCreateReqDTO in);

    @AfterMapping
    default void afterMapping(SwitchCabinetCreateReqDTO in, @MappingTarget SwitchCabinet entity) {
        if (in.getBusbarMaterial() != null) {
            entity.setBusbarMaterial(BusbarMaterial.of(in.getBusbarMaterial()));
        }
        if (in.getProtectionLevel() != null) {
            entity.setProtectionLevel(ProtectionLevel.of(in.getProtectionLevel()));
        }
    }

    @Mapping(target = "busbarMaterial", ignore = true)
    @Mapping(target = "protectionLevel", ignore = true)
    void updateEntity(SwitchCabinetUpdateReqDTO in, @MappingTarget SwitchCabinet entity);

    @AfterMapping
    default void afterMapping(SwitchCabinetUpdateReqDTO in, @MappingTarget SwitchCabinet entity) {
        if (in.getBusbarMaterial() != null) {
            entity.setBusbarMaterial(BusbarMaterial.of(in.getBusbarMaterial()));
        }
        if (in.getProtectionLevel() != null) {
            entity.setProtectionLevel(ProtectionLevel.of(in.getProtectionLevel()));
        }
    }
}